define(function(require, exports, module) {

  var Backbone = require('backbone');
  var $ = require('jquery');
  var _ = require('underscore');
  var jsonview = require('jsonview');
  var tmpl = require('text!../tmpl.html');

  var colors = env.colors;
  var i = 0

  function main() {
    var $app = $('<div id="app">');
    $('#main').html($app);

    $app.append(tmpl);

    $('.wrap').scroll(scroll)

    scroll()
    scroll()
  }

  function scroll() {
    var wrapElem = $('.wrap')[0]
    var mainElem = $('.main')[0]

    var wrapHeight = wrapElem.clientHeight;
    var mainHeight = mainElem.clientHeight;

    var wrapScrollTop = wrapElem.scrollTop

    console.log(wrapHeight, mainHeight, wrapScrollTop)

    if (mainHeight - wrapScrollTop <= wrapHeight) {
      $('.main').append($('<div>').addClass('h300').css('background-color', colors[i++]))
    }
  }

  return main;
})
